package com.wrb.manage.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wrb.manage.model.Appointment;
import com.wrb.manage.response.ResponseCode;
import com.wrb.manage.response.ServerResponse;
import com.wrb.manage.service.AppointmentService;
import com.wrb.manage.utils.SnowflakeIdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wrb
 * @date 2019/12/6 11:42
 */
@RestController
@RequestMapping("/manage/appointment")
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(tags = "预约管理")
public class AppointmentController {

	@Autowired
	private AppointmentService appointmentService;

	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "新增预约")
	public ResponseEntity add(
			@RequestBody Appointment appointment
	) {
		appointment.setAppointmentId(SnowflakeIdWorker.generateId());
		int result = this.appointmentService.insertAppointment(appointment);
		if (result == 0) {
			return ServerResponse.error(ResponseCode.DB_ERROR, "新增失败");
		}
		return ServerResponse.success();
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "预约管理列表")
	public ResponseEntity list(
			@ApiParam(required = false, value = "页码") @RequestParam(required = false, defaultValue = "1") Integer pageNo,
			@ApiParam(required = false, value = "每页数量") @RequestParam(required = false, defaultValue = "20") Integer pageSize
	) {
		Page<Object> page = PageHelper.startPage(pageNo, pageSize);
		List list = this.appointmentService.getList();
		return ServerResponse.successWithPageHelper(list, page);
	}
}
